let isEmpty = function (val) {
  if (val == null || val == undefined) {
    return true;
  }
  if (type.isArray(val) && val.length == 0) {
    return true;
  } else if (type.isObject(val) && Object.keys(val).length == 0) {
    return true;
  } else if (type.isString(val) && val.trim().length == 0) {
    return true;
  }
  return false;
}
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                 //月份
    "d+": this.getDate(),                    //日
    "h+": this.getHours(),                   //小时
    "m+": this.getMinutes(),                 //分
    "s+": this.getSeconds(),                 //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds()             //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

const dateFormat = function (date, pattern) {
  var d = new Date(date).Format("yyyy-MM-dd hh:mm:ss");
  if (pattern) {
    d = new Date(date).Format(pattern);
  }
  return d.toLocaleString();
}

class TypeUtil {
  static OBJECT = "object"
  static ARRAY = "array"
  static UNDEFINED = "undefined"
  static FUNCTION = "function"
  static SYMBOL = "symbol"
  static DATE = "date"
  static REGEX = "regex"
  static DOCUMENT = "document"
  static GLOBAL = "global"
  static NULL = "null"


  static STRING = "string"
  static NUMBER = "number"
  static BOOLEAN = "boolean"

  isObject = function (val) {
    return Object.prototype.toString.call(val) === '[object Object]';
  }
  isArray = function (val) {
    return Object.prototype.toString.call(val) === '[object Array]';
  }
  isUndefined = function (val) {
    return Object.prototype.toString.call(val) === '[object Undefind]';
  }
  isNull = function (val) {
    return Object.prototype.toString.call(val) === '[object Null]';
  }
  isString = function (val) {
    return Object.prototype.toString.call(val) === '[object String]';
  }
  isNumber = function (val) {
    return Object.prototype.toString.call(val) === '[object Number]';
  }
  isBoolean = function (val) {
    return Object.prototype.toString.call(val) === '[object Boolean]';
  }
  isSymbol = function (val) {
    return Object.prototype.toString.call(val) === '[object Symbol]';
  }
  isFunction = function (val) {
    return Object.prototype.toString.call(val) === '[object Function]';
  }
  isDate = function (val) {
    return Object.prototype.toString.call(val) === '[object Date]';
  }
  isRegex = function (val) {
    return Object.prototype.toString.call(val) === '[object Regex]';
  }
  isDocument = function (val) {
    return Object.prototype.toString.call(val) === '[object HTMLDocument]';
  }
  isGlobal = function (val) {
    return Object.prototype.toString.call(val) === '[object Global]';
  }
  getType = function (val) {
    const type = Object.prototype.toString.call(val);
    return type.replace("[object ", "").replace("]", "").toLowerCase();
  }
}

class FormDataUtil {
  getFormData = function (json, prefix) {
    let fd = new FormData();
    let formJson = this.getFormDataJson(json, prefix);
    Object.keys(formJson).forEach(key => {
      fd.append(key, formJson[key]);
    })
    return fd;
  }
  getFormDataJson = function (json, prefix) {
    let formJson = {};
    if (!isEmpty(json) && type.isObject(json)) {
      let nfj = this.transformObject(json, prefix)
      Object.assign(formJson, nfj);
    } else if (type.isArray(json) && json.length > 0) {
      let nfj = this.transformArray(json, prefix)
      Object.assign(formJson, nfj);
    } else {
      throw "JSON转换FormData数据失败，源数据不是JSON对象或数组";
    }
    return formJson;
  }
  transformArray = function (array, pk = null) {
    let formJson = {};
    for (let i = 0; i < array.length; i++) {
      let item = array[i];
      let nk = isEmpty(pk) ? "[" + i + "]" : pk + "[" + i + "]";
      if (type.isObject(item) || type.isArray(item)) {
        let nfj = this.getFormDataJson(item, nk)
        Object.assign(formJson, nfj);
      } else {
        formJson[nk] = item;
      }
    }
    return formJson;
  }

  transformObject = function (json, pk = null) {
    let formJson = {};
    Object.keys(json).forEach((key) => {
      let nk = isEmpty(pk) ? key : pk + '.' + key;
      let val = json[key];
      if (type.isObject(val) || type.isArray(val)) {
        let nfj = this.getFormDataJson(val, nk)
        Object.assign(formJson, nfj);
      } else {
        formJson[nk] = val;
      }
    });
    return formJson;
  }

}

const type = new TypeUtil();

const formDate = new FormDataUtil();
export default {Type: type, FormData: formDate, isEmpty, dateFormat}
